package com.review.test;

import java.text.DecimalFormat;
import java.util.*;
/**
 * 红包算法
 */

public class Redpacket {
    Random random = new Random();

    public List<Float> GiveRedPack(float TotalMoney, int TotalPeople) {
        float min=0.01f;
        float max;
        List list= new ArrayList();
        DecimalFormat dcmFmt = new DecimalFormat("0.00");
        if (TotalMoney / TotalPeople > 0.01 && TotalPeople > 0) {
            for (int i = 1; i<=TotalPeople - 1; i++) {
                max=Float.parseFloat(dcmFmt.format((TotalMoney - ((TotalPeople- i) * 0.01f))));
                float money = (random.nextFloat() *(max-min))+min;
                list.add(dcmFmt.format(money));
                money=Float.parseFloat(dcmFmt.format(money));
                TotalMoney = TotalMoney - money;
            }
            list.add(dcmFmt.format(TotalMoney));
            Collections.shuffle(list);
        }
        return list;
    }
}
